package com.alstudio.view.pullscrollview;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.Scroller;

import com.alstudio.utils.pixel.PixelUtil;

public class MyScrollViewForPath extends ScrollView {

	private Context mContext;
	private Scroller mScroller;
	TouchTool tool;
	int left, top;
	float startX, startY, currentX, currentY;
	int bgViewH, iv1W;
	int rootW, rootH;

	View headView;
	View bgView;
	View mainView;
	boolean scrollerType;
	static final int len = 0xc8;

	private ScrollChangeCallback mCallback;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			bgView.setLayoutParams(new FrameLayout.LayoutParams(
					FrameLayout.LayoutParams.MATCH_PARENT, bgViewH));
		};
	};

	FrameLayout.LayoutParams ll_params;

	public MyScrollViewForPath(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

	}

	public MyScrollViewForPath(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		mScroller = new Scroller(mContext);
	}

	public MyScrollViewForPath(Context context) {
		super(context);

	}

	public void setScrollChangeCallback(ScrollChangeCallback callback) {
		mCallback = callback;
	}

	public void setParams(View headview, View bgView, View mainView) {
		headView = headview;
		this.bgView = bgView;
		this.mainView = mainView;
		ll_params = (FrameLayout.LayoutParams) mainView.getLayoutParams();
	}

	float lastY = 0, curY = 0;

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		int action = event.getAction();
		if (!mScroller.isFinished()) {
			return super.onTouchEvent(event);
		}
		currentX = event.getX();
		currentY = event.getY();
		headView.getTop();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			left = bgView.getLeft();
			top = bgView.getBottom();
			rootW = getWidth();
			rootH = getHeight();
			bgViewH = bgView.getHeight();
			startX = currentX;
			startY = currentY;
			tool = new TouchTool(bgView.getLeft(), bgView.getBottom(),
					bgView.getLeft(), bgView.getBottom() + len);
			break;
		case MotionEvent.ACTION_MOVE:
			int dif = (int) (currentY - lastY);
			if (mCallback != null) {
//				mCallback.onMove(dif);
				mCallback.onMove(PixelUtil.dp2px(dif,mContext));
			}

			if (headView.isShown() && headView.getTop() >= 0) {
				if (tool != null) {
					int t = tool.getScrollY(currentY - startY);
					if (t >= top && t <= headView.getBottom() + len) {

						bgView.setLayoutParams(new FrameLayout.LayoutParams(
								FrameLayout.LayoutParams.MATCH_PARENT, t));

						int marginTop = tool.getMarginTop(currentY - startY);
						FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
								FrameLayout.LayoutParams.MATCH_PARENT,
								FrameLayout.LayoutParams.MATCH_PARENT);
						params.gravity = Gravity.NO_GRAVITY;
						params.topMargin = marginTop;
						mainView.setLayoutParams(params);

					}
				}
				scrollerType = false;
			}

			break;
		case MotionEvent.ACTION_UP:
			if (mCallback != null) {
//				mCallback.onMove(-140);
				mCallback.onMove(PixelUtil.dp2px(-140,mContext));
			}
			scrollerType = true;
			mScroller.startScroll(bgView.getLeft(), bgView.getBottom(),
					0 - bgView.getLeft(), bgViewH - bgView.getBottom(), 200);
			invalidate();

			break;
		}
		lastY = event.getY();
		return super.dispatchTouchEvent(event);
	}

	public void computeScroll() {
		super.computeScroll();
		if (mScroller.computeScrollOffset()) {
			int x = mScroller.getCurrX();
			int y = mScroller.getCurrY();
			bgView.layout(0, 0, rootW, y);
			mainView.setLayoutParams(ll_params);
			
			// 重点
			if (!mScroller.isFinished() && scrollerType) {// 重点判断
			 bgView.setLayoutParams(new FrameLayout.LayoutParams(
			 FrameLayout.LayoutParams.MATCH_PARENT, bgViewH));
//				handler.sendEmptyMessageDelayed(0, 200);
			}

			invalidate();



		}
	}

	public interface ScrollChangeCallback {
		public void onMove(int t);

		public void onScrollUp();
	}
}
